const app = getApp()
const examApi = require('../../utils/examApi.js')

Page({
  data: {
    mistake: null,
    options: {},
    loading: true
  },

  onLoad(options) {
    console.log('错题详情页加载，接收到的参数:', options);
    
    if (!options.id) {
      console.error('错题ID缺失');
      wx.showToast({
        title: '错题ID缺失',
        icon: 'none'
      });
      setTimeout(() => {
        wx.navigateBack();
      }, 1500);
      return;
    }
    
    const mistakeId = parseInt(options.id);
    console.log('将获取错题ID:', mistakeId);
    this.fetchMistakeDetail(mistakeId);
  },
  
  // 从服务器获取错题详情
  fetchMistakeDetail(mistakeId) {
    console.log('开始获取错题详情...');
    wx.showLoading({
      title: '加载中',
      mask: true
    });
    
    examApi.getMistakeById(mistakeId)
      .then(mistake => {
        console.log('获取到的错题详情:', mistake);
        
        try {
          // 解析选项
          let optionsObj = {};
          if (mistake.options) {
            try {
              optionsObj = JSON.parse(mistake.options);
            } catch (error) {
              console.error('选项解析失败，尝试其他解析方式', error);
              // 尝试直接使用选项（可能已经是对象）
              if (typeof mistake.options === 'object') {
                optionsObj = mistake.options;
              }
            }
          }
          
          console.log('解析后的选项:', optionsObj);
          
          this.setData({
            mistake,
            options: optionsObj,
            loading: false
          });
        } catch (error) {
          console.error('解析选项失败:', error);
          this.setData({
            mistake,
            options: {},
            loading: false
          });
        }
      })
      .catch(error => {
        console.error('获取错题详情失败:', error);
        wx.showToast({
          title: '获取详情失败',
          icon: 'none'
        });
        setTimeout(() => {
          wx.navigateBack();
        }, 1500);
      })
      .finally(() => {
        wx.hideLoading();
      });
  },

  // 返回错题列表
  backToList() {
    wx.navigateBack();
  },

  // 删除当前错题
  deleteMistake() {
    const { mistake } = this.data;
    if (!mistake) return;

    wx.showModal({
      title: '确认删除',
      content: '确定要删除这道错题吗？',
      success: (res) => {
        if (res.confirm) {
          const token = wx.getStorageSync('token');
          wx.showLoading({
            title: '删除中',
            mask: true
          });
          
          examApi.deleteMistake(mistake.id)
            .then(() => {
              wx.showToast({
                title: '删除成功',
                icon: 'success'
              });
              // 返回上一页
              setTimeout(() => {
                wx.navigateBack();
              }, 1500);
            })
            .catch(error => {
              console.error('删除错题失败:', error);
              wx.showToast({
                title: '删除失败',
                icon: 'none'
              });
            })
            .finally(() => {
              wx.hideLoading();
            });
        }
      }
    });
  }
}) 